package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.pojo.constant.SystemConstantEnum;
import com.team.recruitment_management_system.pojo.entity.SysUserRole;
import com.team.recruitment_management_system.service.SysUserRoleService;
import com.team.recruitment_management_system.mapper.SysUserRoleMapper;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 86147
* @description 针对表【sys_user_role(用户角色表)】的数据库操作Service实现
* @createDate 2024-03-21 20:28:40
*/
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>
    implements SysUserRoleService{

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<Void> addUserSeekerRole(String token) {
        int userId = jwtHelper.getUserId(token);
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId,userId)
                .eq(SysUserRole::getRoleId,SystemConstantEnum.ROLE_SEEKER.getValue());
        if(this.exists(queryWrapper)){
            return Result.build(null,500,"已拥有该角色");
        }
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(SystemConstantEnum.ROLE_SEEKER.getValue());
        this.save(userRole);
        return Result.ok(null);
    }

    @Override
    public Result<Void> addUserHRRole(String token) {
        int userId = jwtHelper.getUserId(token);
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId,userId)
                .eq(SysUserRole::getRoleId,SystemConstantEnum.ROLE_HR.getValue());
        if(this.exists(queryWrapper)){
            return Result.build(null,500,"已拥有该角色");
        }
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(SystemConstantEnum.ROLE_HR.getValue());
        this.save(userRole);
        return Result.ok(null);
    }
}




